package com.snatch.snatchmgr.core;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.snatch.snatchmgr.system.SystemData;
import com.snatch.snatchmgr.vo.WebSnatchInfo;

/**
 * 定期处理已经失去连接的抓取节点并且报警
 */
@Component
public class RemoveNodeDaemon extends Thread {

	// 日志对象
	private Logger log = Logger.getLogger(RemoveNodeDaemon.class);
	private Object obj = new Object();
	
	/**
	 * 移除任务间隔时间
	 */
	private long removeTime = 15*1000;
	
	private long removeOutTime = 32*1000;

	@Override
	public void run() {
		while (true) {
			synchronized (obj) {
				try {
					obj.wait(removeTime);
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			try {
				Iterator<CopyOnWriteArrayList<WebSnatchInfo>>  iterator = SystemData.webSnachInfos.values().iterator();
				while (iterator.hasNext()) {
					CopyOnWriteArrayList<WebSnatchInfo> list = iterator.next();
					for(int i=0;i<list.size();i++){
						WebSnatchInfo webSnatchInfo = list.get(i);
						if((webSnatchInfo.getUpdateSystemTime()+removeOutTime)<System.currentTimeMillis()){
							webSnatchInfo.setValid(1);
							log.info("过期移除节点："+webSnatchInfo.getOnlyCode()+" "+webSnatchInfo.getWebName());
						}
					}
					
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
